// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#pragma once

#include <memory>
#include <string>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/ftx_common/api_util/auth_key.h"
#include "coin2/exchange/ftx_common/api_util/rate_tracker.h"
#include "coin2/exchange/order/ftx.pb.h"

namespace coin2::exchange::ftx_common::api_order {

enum FtxOrderSubTopicId : int { REST_DEPOSIT = 10000, REST_WITHDRAWAL = 10001 };

}  // namespace coin2::exchange::ftx_common::api_order

class FtxNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using RestParams = coin2::exchange::order::FtxRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using FtxOrderSubTopicId = coin2::exchange::ftx_common::api_order::FtxOrderSubTopicId;
  using AsioHttpContextList = std::list<std::shared_ptr<AsioHttpContext>>;

 public:
  std::shared_ptr<AsioHttpContext> SubmitOrderExecution(const OrderExecutionProto& order);
  std::shared_ptr<AsioHttpContext> SubmitRestQuery(const RestQueryProto& query);
  AsioHttpContextList SubmitRestQueryList(const RestQueryProto& query);

 public:
  explicit FtxNativePrivateClient(const MarketExchangeApi& mea, const NativeClientConfig& config);

  std::shared_ptr<AsioHttpContext> QueryBalance();
  std::shared_ptr<AsioHttpContext> QueryPosition();
  std::shared_ptr<AsioHttpContext> QueryAccount();
  std::shared_ptr<AsioHttpContext> QueryOpenOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryTrades(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryDeposits(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryWithdraws(const RestParams& params);


  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelAllOrder(const RestParams& params);

  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&) override;

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);

 private:
  RoundRobinRestAuth order_auths_;
};
